package com.baiytfp.hf.HFDataExchange.action.dao;

import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.baiytfp.framework.base.BaseDao;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class MenuDAOImpl extends BaseDao {
	public List<Map> listMenu(String treeType, String objCode, String objType) throws Exception {
		StringBuffer sWhere = new StringBuffer();
		List params = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append("select t1.*, attr.menu_name, attr.en_name, attr.url, attr.icon,  ");
		sql.append(" a.node_seq, a.node_seq_code, a.tree_type,a.is_auth,a.is_disabled,  a.id as is_edit ");
		sql.append(" from ");
		sql.append(TableName.TREE + " t1 left join ");
		sql.append(TableName.MENU + " attr on t1.id=attr.tree_id ");
		sql.append(" left join "+ TableName.AUTH + " a on attr.tree_id=a.node_seq ");

		if (sWhere.length() > 0) {
			sWhere.append(" and ");
		}
		sWhere.append(" t1.TREE_TYPE=? and (IS_OPEN=1 OR IS_OPEN IS NULL) ");
		params.add(treeType);

		sWhere.append(" and a.obj_type=? and a.obj_code like ?  ");
		params.add(objType);
		params.add(objCode);

		// String orderBy =
		// " order by node_deep desc, parent_seq_code, node_order asc "; //
		// 先把父结点取出来
		String orderBy = " order by node_deep asc, parent_tree_code, node_order asc "; // 先把父结点取出来

		System.out.println(sql + " where " + sWhere.toString() + orderBy);

		List<Map> list = HFDBHelper.querySql(sql + " where " + sWhere.toString() + orderBy, Map.class, params.toArray());

		return list;
	}
}
